<?php
/**
 * @author writethesky <writethesky@163.com>
 */


$max_file_size = 500000;              //上传文件大小限制，单位BYTE


$config = require_once "./config.php";

$base64_date = $_POST["base64Date"];
$base64_date_arr = explode(";", $base64_date);

if(count($base64_date_arr) < 2){
	echo "上传的数据异常";
	exit();
}

$file_format_arr = explode(":", $base64_date_arr[0]);
if(count($file_format_arr) < 2){
	echo "上传的数据异常";
	exit();
}



if (!in_array(end($file_format_arr), $config['UPTYPES'])) {        //判断文件的类型
    echo '上传文件类型不符' . $type;
    exit ();
}


$img_base64_data = end(explode(",", end($base64_date_arr)));
$img_data = base64_decode($img_base64_data);

if ($config['MAX_FILE_SIZE'] < strlen($img_data)) { //判断文件的大小
    echo '上传文件太大';echo strlen($img_data);
    exit ();
}

if (!file_exists($config['DESTINATION_FOLDER'])) {
    mkdir($config['DESTINATION_FOLDER']);
}
$ftype = end(explode("/", end($file_format_arr)));

$destination = $config['DESTINATION_FOLDER'] . time(). "." . $ftype;

file_put_contents($destination, $img_data);
// echo $destination;  //" 已经成功上传 文件名: " . 

$tmparr = explode("/", $_SERVER['PHP_SELF']);
array_pop($tmparr);
echo ($_SERVER['HTTPS'] != "on" ? "http" : "https") . "://" . $_SERVER['HTTP_HOST'] . implode($tmparr, "/") . "/" . $destination;
	
if ($config['WATERMARK'] == 1) {
    require_once "water.php";
}